package com.guchenbo.example.lock.reentrant;

import java.util.concurrent.locks.ReentrantLock;
/**
 * 用来debug跟踪下ReentrantLock的源码，以便更好的理解
 * <p/>
 * Created by CreGu on 2016/6/21.
 */
public class TestLock {

    private ReentrantLock reentrantLock = new ReentrantLock(true);
//    private java.util.concurrent.locks.ReentrantLock reentrantLock = new ReentrantLock();

    public void lock() {
        try {
            System.out.println(Thread.currentThread());
            reentrantLock.lock();

            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } finally {
            reentrantLock.unlock();
        }
    }

    public static void main(String[] args) {
        final TestLock test = new TestLock();

        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                test.lock();
            }
        },"t1");

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                test.lock();
            }
        },"t2");

        t1.start();
        //t2.start();
    }
}
